import time
from machine import Pin,PWM
import network

def do_connect():
    wlan = network.WLAN(network.WLAN.IF_STA)
    wlan.active(True)
    # 如果未连接，则进行连接
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('KDGC-HLY', '055166775656')
        # 等待连接成功
        while not wlan.isconnected():
            blink()
            #  在Python中，pass 是一个占位语句，表示“什么都不做”。
            # pass
    else:
        print('already connected')
    print('network config:', wlan.ipconfig('addr4'))


# 呼吸灯
def breatheLed():
# 实现呼吸灯
    led2 = PWM(Pin(2))
    led2.freq(1000)

    while True:  # 控制整体的重复 （1.从不亮到亮 2.从亮到不亮）
        # 1.实现从不亮到亮
        print('实现从不亮到亮')
        for i in range(0, 1024):
            led2.duty(i)
            time.sleep_ms(1)  # 每隔1ms变化一次亮度

        # 2.实现从亮到不亮
        print('实现从亮到不亮')
        for i in range(1023, -1, -1):
            led2.duty(i)
            time.sleep_ms(1)


#  闪烁
def blink():
    # 实现灯闪烁
    print('实现灯闪烁')
    # 引脚2为输出引脚
    led = Pin(2, Pin.OUT)

    while 1:
        led.value(1)
        time.sleep(1)
        led.value(0)
        time.sleep(1)



if __name__ == '__main__':
    do_connect()
    breatheLed()
